/* 
	ZClub Coffee
*/
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "freertos/event_groups.h"
#include "freertos/queue.h"
#include "esp_system.h"
#include "driver/gpio.h"
#include "nvs_flash.h"
#include "esp_wifi.h"
#include "esp_wpa2.h"
#include "esp_event.h"
#include "esp_netif.h"
#include "esp_smartconfig.h"
#include "esp_websocket_client.h"
#include "driver/ledc.h"
// #include "esp_err.h"
// #include "esp_log.h"
// #include "esp_task_wdt.h"

// LVGL
#include "lvgl/lvgl.h"
#include "lvgl_esp32_drivers/lvgl_helpers.h"
// 旋转编码器
#include "rotary_encoder/include/rotary_encoder.h"


// 项目文件
#include "coffee_lib/config.h"
#include "coffee_lib/var.h"
#include "coffee_lib/status.h";
#include "coffee_lib/data.h"
#include "coffee_lib/util.h"
#include "coffee_lib/code.h"
#include "ui/ui.h"
#include "coffee_lib/rot.h"
#include "coffee_lib/bee.h"
#include "coffee_lib/ws.h"
#include "coffee_lib/key.h"
#include "coffee_lib/nvs.h"
#include "coffee_lib/wifi.h"
#include "coffee_lib/event.h"

void sysUtil();

void app_main() {
	wsDataQ = xQueueCreate(20, sizeof(char *));
	inputQ = xQueueCreate(20, sizeof(char *));
	eventInit();
	
	// LVGL UI初始化
	xTaskCreatePinnedToCore(uiInitTask, "uiInitTask", 1024 * 5, NULL, 0, NULL, 1);
	// 初始化电键
	xTaskCreatePinnedToCore(keyInitTask, "keyInitTask", 1024 * 3, NULL, 0, NULL, 1);
	// 初始化旋转编码器
	xTaskCreatePinnedToCore(rotInit, "rotInit", 1024 * 3, NULL, 0, NULL, 1);
	
	
	
	// xTaskCreate(sysUtil, "sysUtil", 1024 * 3, NULL, 0, NULL);
}


// 调试使用内容 开始
void sysUtil() {
	// char *buf;
	// int handlerCont = 3;
	// TaskHandle_t handleList[] = {rotInitH, keyInitH, uiInitH};
	// TaskHandle_t handleList[] = {0};
	while(1) {
		// USE_TRACE_FACILITY
		// USE_STATS_FORMATTING_FUNCTIONS
		// for(int i = 0 ; i < handlerCont; i++)
		// {
		// 	char *taskTag = pcTaskGetName(handleList[i]);
		// 	UBaseType_t cStack = uxTaskGetStackHighWaterMark(handleList[i]);
		// 	ESP_LOGW("SYSTEM DEBUG", "%s\tStack = %d",taskTag, cStack);
		// }
		
		// vTaskList(&buf);
		// printf("%s\n", buf);

		char *pbuffer = (char *)calloc(1, 2048);
		printf("===================== heap:%u =====================\r\n", esp_get_free_heap_size());
		printf("name\t\tstatus\tPriority\tstack\n");
		printf("---------------------------------------------------\r\n");
		vTaskList(pbuffer);
		printf("%s", pbuffer);
		printf("===================================================\r\n");
		free(pbuffer);

		vTaskDelay(pdMS_TO_TICKS(1000));
	}
	vTaskDelete(NULL);
}
// 调试使用内容 结束